<?php

namespace App\Http\Controllers;

use App\Model\Commodity;
use App\Model\Commodity_info;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Support\Facades\DB;


class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

    public function error_url($url = '/', $message = '未知错误', $time = 3)
    {
        return view('errors.error', ['url' => $url, 'message' => $message, 'time' => $time]);
    }

    /**
     * 方法 : trans_begin
     * @User wuqin
     * @DateTime 2018-03-13 19:28
     */
    public function trans_begin()
    {
        DB::beginTransaction();
    }


    /**
     * 方法 : trans_rollback
     * @User wuqin
     * @DateTime 2018-03-13 19:28
     */
    public function trans_rollback()
    {
        DB::rollBack();
    }

    /**
     * 方法 : trans_commit
     * @User wuqin
     * @DateTime 2018-03-13 19:28
     */
    public function trans_commit()
    {
        DB::commit();
    }

    public function index_data()
    {
        $ids = [72, 76, 64, 78];
        $commodityModel = new Commodity();
        $commodity = $commodityModel->get_commodity_by_commidity_ids($ids);
//        dd($commodity);
        if (!empty($commodity)) {
            $infoModel = new Commodity_info();
            foreach ($commodity as $key => $value) {
                $keys = $infoModel->get_info_keys_by_commodity((int)$value["commodity_id"]);
                preg_match('/(?<=commodity_info:).*?(?=(:commodity))/', $keys[0], $matches);
                if (empty($matches) || $matches[0] == '0') {
                    unset($commodity[$key]);
                    continue;
                }
                $res = $infoModel->mget_array_redis($keys);
                if (empty($res) || count($res) == 0)
                    unset($commodity[$key]);
                $commodity[$key]['commodity_info_price'] = reset($res)['commodity_info_price'];
            }
        } else {
            $commodity = array();
        }
        return $commodity;
    }

    public function index_banner()
    {
        $ids = [
            [
                "id" => 72,
                "uri" => "/img/index/pic_001.jpeg"
            ],
            [
                "id" => 76,
                "uri" => "/img/index/pic_002.jpeg"
            ],
            [
                "id" => 64,
                "uri" => "/img/index/pic_003.jpeg"
            ],
            [
                "id" => 78,
                "uri" => "/img/index/pic_004.jpeg"
            ]
        ];
        return $ids;
    }
}
